package com.qdaily.ui.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.qdaily.supportlibrary.pulltorefresh.PtrDefaultHandler;
import com.qdaily.supportlibrary.pulltorefresh.PtrFrameLayout;
import com.qdaily.supportlibrary.pulltorefresh.PtrHandler;
import com.qdaily.supportlibrary.pulltorefresh.PtrUIRefreshCompleteHandler;
import com.qdaily.supportlibrary.pulltorefresh.header.StoreHouseHeader;
import com.qdaily.supportlibrary.pulltorefresh.loadmore.LoadMoreContainer;
import com.qdaily.supportlibrary.pulltorefresh.loadmore.LoadMoreExpandableListViewContainer;
import com.qdaily.supportlibrary.pulltorefresh.loadmore.LoadMoreHandler;
import com.qdaily.supportlibrary.util.LocalDisplay;
import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.R;

/**
 * Created by song on 15/5/14.
 */
public class QDRefreshCommentView extends PtrFrameLayout {
    private ExpandableListView mExpandableListView;
    private OnRefreshAndLoadMoreListener onRefreshAndLoadMoreListener;
    private LoadMoreExpandableListViewContainer loadMoreExpandableListViewContainer;
    private StoreHouseHeader header;
    private QDLoadMoreView footView;

    public QDRefreshCommentView(Context context) {
        this(context, null);
    }

    public QDRefreshCommentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QDRefreshCommentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        loadMoreExpandableListViewContainer = (LoadMoreExpandableListViewContainer) LayoutInflater.from(getContext()).inflate(R.layout.view_refreshandloadmoreexplandablelistview, null);
        this.addView(loadMoreExpandableListViewContainer);

        mExpandableListView = (ExpandableListView) loadMoreExpandableListViewContainer.findViewById(R.id.loadMoreExplandableListView);
        mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
        mExpandableListView.setGroupIndicator(null);


        this.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mExpandableListView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (onRefreshAndLoadMoreListener != null) {
                    onRefreshAndLoadMoreListener.onRefreshBegin();
                }
            }
        });

        loadMoreExpandableListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                QLog.w("QDRefreshCommentView", "onLoadMore");
                if (onRefreshAndLoadMoreListener != null) {
                    if (footView != null)
                        footView.start();
                    onRefreshAndLoadMoreListener.onLoadMore();
                }
            }
        });
    }

    public void configHeadview()
    {
        header = new StoreHouseHeader(getContext());
        header.setPadding(0, LocalDisplay.dp2px(15), 0, LocalDisplay.dp2px(15));
        header.initWithString("Qdaily 2603");

        this.setHeaderView(header);
        this.addPtrUIHandler(header);


        this.setLoadingMinTime(1000);
    }

    public ExpandableListView getExpandableListView() {
        return mExpandableListView;
    }

    /**
     * 是否还有下一页数据， 在onUIRefreshComplete调用，切记。
     */
    public void hasMoreLoadData(boolean emptyResult, boolean hasMore) {
        loadMoreExpandableListViewContainer.loadMoreFinish(emptyResult, hasMore);

        if (!hasMore)
        {
            if (footView != null)
            {
                footView.stop();
                footView.setVisibility(GONE);
                loadMoreExpandableListViewContainer.removeFooterView(footView);
                footView = null;
            }
        }
        else
        {
            if (footView == null)
            {
                footView = new QDLoadMoreView(getContext());
                footView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
                this.setLoadingMinTime(1000);
                loadMoreExpandableListViewContainer.setLoadMoreView(footView);
            }
            footView.pause();
        }
    }

    /**
     * 设置为空界面
     *
     * @param emptyView
     */
    public void setEmptyView(final View emptyView) {
        if (mExpandableListView != null) {
            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(-1, -1);
            mExpandableListView.setLayoutParams(ll);
            ((ViewGroup) mExpandableListView.getParent()).addView(emptyView);
            mExpandableListView.setEmptyView(emptyView);
        }
    }

    /**
     * 设置条目点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClick(AdapterView.OnItemClickListener onItemClickListener) {
        if (mExpandableListView != null) {
            mExpandableListView.setOnItemClickListener(onItemClickListener);
        }
    }

    /**
     * 设置条目长点击事件
     *
     * @param onItemLongClickListener
     */
    public void setOnItemLongClick(AdapterView.OnItemLongClickListener onItemLongClickListener) {
        if (mExpandableListView != null) {
            mExpandableListView.setOnItemLongClickListener(onItemLongClickListener);
        }
    }

    /**
     * 设置是否自动刷新
     *
     * @param isAutoRefresh
     */
    public void setAutoRefresh(final boolean isAutoRefresh) {
        if (this != null) {
            this.postDelayed(new Runnable() {
                @Override
                public void run() {
                    QDRefreshCommentView.this.autoRefresh(isAutoRefresh);
                }
            }, 100);
        }
    }

    /**
     * 设置adapter
     *
     * @param adapter
     */
    public void setAdapter(BaseExpandableListAdapter adapter) {
        if (mExpandableListView != null) {
            mExpandableListView.setAdapter(adapter);
        }
    }

    public void addHeaderView(View view) {
        if (mExpandableListView != null) {
            mExpandableListView.addHeaderView(view, null, false);
        }
    }

    /**
     * 设置回调完成
     */
    public void onRefreshComplete() {
        this.refreshComplete();
    }

    /**
     * 设置回调监听
     *
     * @param onRefreshAndLoadMoreListener
     */
    public void setOnRefreshAndLoadMoreListener(OnRefreshAndLoadMoreListener onRefreshAndLoadMoreListener) {
        this.onRefreshAndLoadMoreListener = onRefreshAndLoadMoreListener;
    }
}
